package com.shigu.task.service.impl;

import com.alibaba.fastjson.JSON;
import com.opentae.data.mall.beans.MallTask;
import com.opentae.data.mall.beans.UserTask;
import com.opentae.data.mall.pojo.UserTaskDto;
import com.opentae.data.mall.examples.MallTaskExample;
import com.opentae.data.mall.interfaces.MallTaskMapper;
import com.opentae.data.mall.interfaces.UserTaskMapper;
import com.shigu.account.enums.MoneyRecordType;
import com.shigu.account.exception.UtilsAccountException;
import com.shigu.account.service.UserAccountService;
import com.shigu.exceptions.WapException;
import com.shigu.task.bo.CreateTaskBo;
import com.shigu.task.bo.SubmitTaskBo;
import com.shigu.task.service.MallTaskService;
import com.shigu.task.vo.ListResponse;
import com.shigu.task.vo.TaskVo;
import com.shigu.util.MoneyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/*
 * create by  过儿  2019-09-20
 *
 */
@Service("mallTaskServiceImpl")
public class MallTaskServiceImpl implements MallTaskService {

    @Autowired
    MallTaskMapper mallTaskMapper;
    @Autowired
    UserTaskMapper userTaskMapper;
    @Autowired
    UserAccountService userAccountService;

    @Override
    public ListResponse<TaskVo> todoTaskList(Long mallUserId) {
        List<MallTask> list = mallTaskMapper.selTodoTask(mallUserId);
        ListResponse<TaskVo> response = new ListResponse<>();
        response.setData(list.stream().map(o->{
            TaskVo vo = new TaskVo();
            vo.setAward(o.getAward());
            vo.setIcon(o.getIcon());
            vo.setTaskId(o.getTaskId()+"");
            vo.setTitle(o.getTitle());
            return vo;
        }).collect(Collectors.toList()));

        return response.success();
    }

    @Override
    public ListResponse<TaskVo> haveDoTaskList(Long mallUserId) {
        List<UserTaskDto> list = mallTaskMapper.selhaveDoTask(mallUserId);
        ListResponse<TaskVo> response = new ListResponse<>();
        response.setData(list.stream().map(o->{
            TaskVo vo = new TaskVo();
            vo.setAward(o.getAward());
            vo.setIcon(o.getIcon());
            vo.setTaskId(o.getTaskId()+"");
            vo.setTitle(o.getTitle());
            vo.setState(o.getState());
            return vo;
        }).collect(Collectors.toList()));

        return response.success();
    }

    @Override
    public synchronized void submitTaskData(SubmitTaskBo bo,Long mallUserId,Long taskId) throws WapException {
        //用户提交，判断是否已提交过了
        if (userTaskMapper.countSubmit(mallUserId,taskId)>0) {
            throw new WapException("您已经提交过这个任务了");
        }

        MallTaskExample example = new MallTaskExample();
        example.setFields("money_award");
        MallTask task = mallTaskMapper.selectByPrimaryKey(taskId);


        UserTask ut = new UserTask();
//        ut.setCatchTime(new Date());
        ut.setSubmitTime(new Date());
        ut.setMallUserId(mallUserId);
        ut.setTaskId(taskId);
        ut.setResultJson(JSON.toJSONString(bo));
        ut.setAward(task.getMoneyAward());
        ut.setStatus(1);
        userTaskMapper.insertSelective(ut);
    }

    @Override
    public void createTask(CreateTaskBo bo) {
        MallTask task = new MallTask();
        task.setTitle(bo.getTitle());
        task.setIcon(bo.getIcon());
        task.setAward(bo.getAward());
        Integer award = MoneyUtil.multipy(bo.getAward(), "100").intValue();
        task.setMoneyAward(award);
//        task.setTaskType(bo.getTaskType());

        mallTaskMapper.insertSelective(task);
    }

    @Override
    public void completeTask(Long catchId) {
        UserTask userTask = userTaskMapper.selectByPrimaryKey(catchId);
        if(userTask!=null){
            userTask.setStatus(2);
            Double money = MoneyUtil.divide(userTask.getAward() + "", "100");
            try {
                userAccountService.addMoney(userTask.getMallUserId(),money,MoneyRecordType.taskAward.getText());
            } catch (UtilsAccountException e) {
                e.printStackTrace();
            }
            userTaskMapper.updateByPrimaryKeySelective(userTask);
        }
    }


}
